package com.wjk.easy.utils;
import android.content.Context;
import android.util.SparseArray;
import android.widget.Toast;

import com.wjk.easy.db.ExamData;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * @author dmrfcoder
 * @date 2018/8/9
 */
public class ExcelUtil {
    private static WritableFont arial14font = null;
    private static WritableCellFormat arial14format = null;
    private static WritableFont arial10font = null;
    private static WritableCellFormat arial10format = null;
    private static WritableFont arial12font = null;
    private static WritableCellFormat arial12format = null;
    private final static String UTF8_ENCODING = "UTF-8";
    /**
     * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
     */
    private static void format() {
        try {
            arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
            arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
            arial14format = new WritableCellFormat(arial14font);
            arial14format.setAlignment(jxl.format.Alignment.CENTRE);
            arial14format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);

            arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
            arial10format = new WritableCellFormat(arial10font);
            arial10format.setAlignment(jxl.format.Alignment.CENTRE);
            arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            arial10format.setBackground(Colour.GRAY_25);

            arial12font = new WritableFont(WritableFont.ARIAL, 10);
            arial12format = new WritableCellFormat(arial12font);
            //对齐格式
            arial10format.setAlignment(jxl.format.Alignment.CENTRE);
            //设置边框
            arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化Excel
     *  @param fileName 导出excel存放的地址（目录）
     * @param size 表的数量
     * @param colName excel中包含的列名（可以有多个）
     */
    public static void initExcel(String fileName, int size, String[] colName) {
        format();
        WritableWorkbook workbook = null;
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            workbook = Workbook.createWorkbook(file);
            //设置表格的名字
            for (int i = 0; i < size; i++) {
                WritableSheet sheet = workbook.createSheet("第"+(i+1)+"组", i);
                //创建标题栏
                sheet.addCell(new Label(0, 0, fileName, arial14format));
                for (int col = 0; col < colName.length; col++) {
                    sheet.addCell(new Label(col, 0, colName[col], arial10format));
                }
                //设置行高
                sheet.setRowView(0, 340);
            }

            workbook.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> void writeObjListToExcel(SparseArray<List<ExamData>> objList, String fileName, D.onExportCompeteListener listener) {
        if (objList != null && objList.size() > 0) {
            WritableWorkbook writebook = null;
            InputStream in = null;
            int total=0;
            try {
                WorkbookSettings setEncode = new WorkbookSettings();
                setEncode.setEncoding(UTF8_ENCODING);
                in = new FileInputStream(new File(fileName));
                Workbook workbook = Workbook.getWorkbook(in);
                writebook = Workbook.createWorkbook(new File(fileName), workbook);

                for(int k=0;k<objList.size();k++){
                    WritableSheet sheet = writebook.getSheet(k);
                    List<ExamData> examDatas = objList.get(objList.keyAt(k));
                    for (int j = 0; j < examDatas.size(); j++) {
                        ExamData projectBean =  examDatas.get(j);
                        List<String> list = new ArrayList<>();
                        list.add(projectBean.getCardId());
                        list.add(projectBean.getWeight()+"");
                        list.add(projectBean.getSizeWidth()+"");
                        list.add(projectBean.getSizeHeight()+"");
                        list.add(projectBean.getSizeOther()+"");
                        list.add(projectBean.getCreateTime()+"");
                        list.add(projectBean.getRemark());
                        list.add(projectBean.getGroupId()+"");

                        for (int i = 0; i < list.size(); i++) {
                            sheet.addCell(new Label(i, j+1, list.get(i), arial12format));
                            if (list.get(i).length() <= 4) {
                                //设置列宽
                                sheet.setColumnView(i, list.get(i).length()+8);
                            } else {
                                //设置列宽
                                sheet.setColumnView(i, list.get(i).length()+5);
                            }
                        }
                        //设置行高
                        sheet.setRowView(j+1, 350);
                        total++;
                    }
                }
                writebook.write();
                listener.onComplete(fileName,total);
            } catch (Exception e) {
                e.printStackTrace();
                listener.onFail(e.getMessage());
            } finally {
                if (writebook != null) {
                    try {
                        writebook.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}

